import 'package:flutter/material.dart';

class MyFormView extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("表单"),
      ),
      body: Container(
        padding: EdgeInsets.only(left: 10,right: 10),
        child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          MyTextField()
        ],
      ),
      )

    );
  }
}
class MyTextField extends StatefulWidget{
  @override
    MyTextFieldState createState() {
      // TODO: implement createState
      return MyTextFieldState();
    }
    
}
class MyTextFieldState extends State<MyTextField>{
  // final textEditingController=TextEditingController();
  // @override
  //   void dispose() {
  //     // TODO: implement dispose
  //     textEditingController.dispose();
  //     super.dispose();
  //   }
  // @override
  //   void initState() {
  //     // TODO: implement initState
  //     super.initState();
  //     // textEditingController.text='ooo';
  //     textEditingController.addListener(
  //       (){
  //         print("${textEditingController.text}");
  //       }
  //     );
  //   }
    final formKey=GlobalKey<FormState>();
    String name,password;
    PrintV (){
      if(formKey.currentState.validate()){
        formKey.currentState.save();
      debugPrint("$name"+"$password");
      Scaffold.of(context).showSnackBar(
        SnackBar(
          content: Text("注册成功$name,$password"),
        )
      );
      }
      else{
        setState(() {
                  autoValue=true;
                });
      }
    }
    String UserValidator(value){
      if(value.isEmpty){
        return '姓名不能为空';
      }
      return null;
    }
    String PassValidator(value){
      if(value.isEmpty){
        return '密码不能为空';
      }
      return null;
    }         
    bool autoValue=false;
  @override
    Widget build(BuildContext context) {
      // TODO: implement build
      return Container(
        child: Form(
          key: formKey,
        
          child: Column(
        children: <Widget>[
          TextFormField(
            decoration: InputDecoration(
              helperText: '',
              labelText: '姓名',
              hintText: "请输入姓名",
              icon: Icon(Icons.supervised_user_circle),
              border: OutlineInputBorder()
            ),
            // onChanged: (value){
            //   print(value);
            // },
            // controller: textEditingController,
            onSaved: (value){
                name=value;
            },
            validator: UserValidator,
            autovalidate: autoValue,          
          ),
          SizedBox(height: 20,),
          TextFormField(
            obscureText: true,
            decoration: InputDecoration(
              helperText: '',
              labelText: '密码',
              hintText: '请输入密码',
              icon: Icon(Icons.pages),
              border: OutlineInputBorder()
            ),
            onSaved: (value){
              password=value;
            },
            autovalidate: autoValue,
            validator: PassValidator,
          ),
          SizedBox(height: 30,),
          Container(
            width: double.infinity,
            child: RaisedButton(
              color: Colors.lightBlue,
              child: Text("登录",style: TextStyle(color: Colors.white),),
              elevation: 0.0,
              onPressed: (){
                PrintV();
              },
            ),
          )
          
        ],
        ),
        ),
      );
    }
}